package com.formina.stars;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.util.FloatMath;

import com.formina.engine.Effect;
import com.formina.engine.Engine;
import com.formina.engine.Model;

public class Comet extends Effect {
	int rotationAxis;
	FloatBuffer fb;
	
	long startTime;
	public Comet(float[] location, float[] scale){
 	   super(location, scale,Engine.Comet);
 	   this.setRotation(new float[]{180f,0,0});  //make it face the camera
 	   this.speed = 2f;
 	   
    }
	   
	   public Comet() {
		  this(new float[]{0,0,0}, new float[]{1f,1f,1f});
	   }
	   
	   @Override
	   public void update(long time){
		   if (startTime == 0){
			   startTime = time;
		   }
		   //setAlpha(Color()[3]-.02f);;
		   setX(getX()+getVelocity()[0]*getSpeed());
		   setY(getY()+getVelocity()[1]* getSpeed());
		   setZ(getZ()+getVelocity()[2]* getSpeed());
		   setAlpha(1f);
		   
		   if(time-startTime > 2000){
			   if(getX()<0){
				   this.isDead = true;
			   }else if(getX()>Engine.WIDTH){
				   this.isDead = true;
			   }else if(getY()<0){
				   this.isDead = true;
			   }else if(getY()>Engine.HEIGHT){
				   this.isDead = true;
			   }
		   }
		   /*
		   Rotation()[rotationAxis] += 4f;
		   if (Rotation()[rotationAxis]>359){
			   Rotation()[rotationAxis]=0;
		   }
		   */
		   
		   if(this.Color()[3] <=0){
			   this.isDead = true;
		   }
	   }
	   
	   @Override
	    public void draw(GL10 gl) {
	    	gl.glPushMatrix();
	    	
	    	gl.glDisable(GL10.GL_LIGHTING);
	        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
			gl.glFrontFace(GL10.GL_CCW); // OpenGL docs
			gl.glEnable(GL10.GL_CULL_FACE); // OpenGL docs
			gl.glCullFace(GL10.GL_BACK); // OpenGL docs
			gl.glEnable(GL10.GL_ALPHA);
			gl.glEnable(GL10.GL_ALPHA_TEST);
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// OpenGL docs.

			
			if(this.modelInfo.TextureCoords.length>0){
				gl.glEnable(GL10.GL_TEXTURE_2D);
				gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

				// Tell OpenGL where our texture is located.
				gl.glBindTexture(GL10.GL_TEXTURE_2D, Engine.Textures[0]);
				
				// Telling OpenGL where our UV coordinates are.
				gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, modelInfo.textureBuffer);
				gl.glTexParameterf(GL10.GL_TEXTURE_2D,
		                   GL10.GL_TEXTURE_MAG_FILTER,
		                   GL10.GL_LINEAR);
			}
			
			gl.glTranslatef(getX(), getY(),getZ());
			gl.glRotatef(Rotation()[0],1f,0,0);
			gl.glRotatef(Rotation()[1],0,1f,0);
			gl.glRotatef(Rotation()[2],0,0,1f);
			gl.glScalef(Scale()[0], Scale()[1], Scale()[2]);
			
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, // OpenGL docs
	                                 modelInfo.vertexBuffer);
			
			gl.glDrawElements(GL10.GL_TRIANGLES, modelInfo.Indices.length,// OpenGL docs
	    		  GL10.GL_UNSIGNED_SHORT, modelInfo.indexBuffer);
	      		      
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	      
			gl.glDisable(GL10.GL_CULL_FACE);
			gl.glDisable(GL10.GL_ALPHA);

			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glDisable(GL10.GL_TEXTURE_2D);
	    	gl.glPopMatrix();
	   }
}
